/**
 * Copyright 2012 Brigham Young University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.byu.nlp.cluster;

import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Iterator;

import com.google.common.base.Preconditions;
import com.google.common.collect.UnmodifiableIterator;

import edu.byu.nlp.data.SparseFeatureVector;
import edu.byu.nlp.pipes.Instance;
import edu.byu.nlp.util.Pair;

/**
 * @author rah67
 *
 */
public class Clusterers {

	private Clusterers() { }
	
	private static class PairedCollection extends
			AbstractCollection<Pair<Instance<Integer, SparseFeatureVector>, Integer>> {

		private final Collection<Instance<Integer, SparseFeatureVector>> data;
		private final int[] predictions;
		
		private PairedCollection(Collection<Instance<Integer, SparseFeatureVector>> data, int[] predictions) {
			this.data = data;
			this.predictions = predictions;
		}

		private class PairedIterator extends
				UnmodifiableIterator<Pair<Instance<Integer, SparseFeatureVector>, Integer>> {
		
			private final Iterator<Instance<Integer, SparseFeatureVector>> it = data.iterator();
			private int i = 0;
			
			@Override
			public boolean hasNext() {
				return i < predictions.length;
			}
		
			@Override
			public Pair<Instance<Integer, SparseFeatureVector>, Integer> next() {
				return Pair.of(it.next(), predictions[i++]);
			}
			
		}
		
		@Override
		public Iterator<Pair<Instance<Integer, SparseFeatureVector>, Integer>> iterator() {
			return new PairedIterator();
		}

		@Override
		public int size() {
			return predictions.length;
		}
		
	}
	
	public static Collection<Pair<Instance<Integer, SparseFeatureVector>, Integer>> pairUp(
			Collection<Instance<Integer, SparseFeatureVector>> data, int[] predictions) {
		Preconditions.checkArgument(data.size() == predictions.length);
		
		return new PairedCollection(data, predictions);
	}
}
